package com.liuwish.learn.algorithm;

/**
 * Description
 * <p>全排列
 * </p>
 * DATE 2018/6/21.
 *
 * @author liuweishi.
 */
public class QuanPaiLie {
    public static final int sum = 13;

    public static void allList(int[] arr, int current, int len) {
        if (current == len - 1) {
            print(arr);
            return;
        }
        for (int i = current; i < len; i++) {
            swap(arr, current, i);
            //if (current==0||((arr[current-1] + arr[current ] < sum)&&(arr[current] + arr[current + 1] < sum))) {
                allList(arr, current + 1, len);
            //}
            swap(arr, current, i);
        }
    }

    public static void swap(int[] arr, int n, int m) {
        int temp = arr[n];
        arr[n] = arr[m];
        arr[m] = temp;
    }

    public static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {4, 5, 7, 6};
        int len = arr.length;
        allList(arr, 0, len);
    }

    public static void arrange(int[] str, int st, int len) {
        if (st == len - 1) {
            for (int i = 0; i < len; i++) {
                System.out.print(str[i] + "  ");
            }
            System.out.println();
        } else {
            for (int i = st; i < len; i++) {
                swap(str, st, i);
                arrange(str, st + 1, len);
                swap(str, st, i);
            }
        }

    }
}
